package com.domor.controller.system;

import com.domor.model.Result;
import com.domor.model.Table;
import com.domor.service.system.RoleService;
import com.domor.utils.CommonUtils;
import com.domor.utils.MapUtils;
import com.domor.utils.ParamUtils;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService service;

    @RequestMapping("/getById")
    public Object getById(int id) {
        return service.getById(id);
    }

    @RequestMapping("/getRoles")
    public Object getRoles(HttpServletRequest request) {
        return Result.success(service.getRoles());
    }

    @RequestMapping("/query")
    public Object query(HttpServletRequest request) {
        Map<String, Object> params = ParamUtils.getParams(request);
        Page page = CommonUtils.startPage(params);
        Table table = new Table(service.query(params), page.getTotal());
        return Result.success(table);
    }

    @RequestMapping("/save")
    public Object save(HttpServletRequest request) {
        Map<String, Object> params = ParamUtils.getParams(request);
        int id = MapUtils.getIntValue(params, "id");
        if (id == 0) {
            service.insert(params);
        } else {
            service.update(params);
        }
        return Result.success();
    }

    @RequestMapping("/delete")
    public Object delete(HttpServletRequest request) {
        Map<String, Object> params = ParamUtils.getParams(request);
        service.delete(params);
        return Result.success();
    }

    @RequestMapping("/getRights")
    public Object getRights(int roleId) {
        return Result.success(service.getRights(roleId));
    }

    @RequestMapping("/saveRights")
    public Object saveRights(String menus, int roleId) {
        service.saveRights(roleId, menus);
        return Result.success();
    }



}
